#include "rtdb.h"

#include "ComStruct.h"

CRTDB::CRTDB(QString keyName /*= "KGDZ_RTDB_BAMS"*/, QObject *parent)
	: QObject(parent)
{
    m_iBattStackNum = MAX_STACK_NUM;    /*电池堆-转发模块数*/
    m_iBattRegNum = MAX_REG_NUM;        /*电池寄存器数*/
    m_iBattBankNum = MAX_BANK_NUM;      /*电池组数*/
    m_iBattAlarmNum = MAX_BANK_NUM;     /*电池告警数*/
    m_iPcsDevNum = MAX_PCS_NUM;         /*PCS 数*/
    m_iMeterDevNum = MAX_METER_NUM;     /*电表数*/

    m_iFireDevNum = MAX_FIRE_NUM;       /*消防设备数*/
    m_iAcDevNum = MAX_AC_NUM;           /*空调设备数*/
    m_iTransDevNum = 2;                 /*变压器设备数*/
    m_iAdamIODevNum = 1;                /*ADAM模块数*/
    m_iIoDevNum = 2;                    /*IO模块数*/

    m_iLcDevNum = MAX_LC_NUM;           /*液冷机数*/
    m_iEcDevNum = MAX_EC_NUM;           /*环控设备数*/
    m_iThDevNum = MAX_TH_NUM;           /*温湿度设备数*/
    m_iDhDevNum = MAX_DH_NUM;           /*除湿设备数*/
    m_iWaterDevNum = MAX_WATER_NUM;     /*水浸设备数*/

    m_iDoorDevNum = MAX_DOOR_NUM;       /*门禁设备数*/
    m_iDcdcDevNum = MAX_DCDC_NUM;       /*DCDC设备数*/
    m_iCpDevNum = MAX_PILE_NUM;         /*充电桩设备数*/
    m_iGasDevNum = MAX_GAS_NUM;         /*可燃气体数*/
    m_iUpsDevNum = MAX_UPS_NUM;         /*UPS数*/

    m_iLedDevNum = MAX_LED_NUM;         /*LED灯数*/
    m_iFanDevNum = MAX_FAN_NUM;         /*风扇数*/
    m_iStsDevNum = MAX_STS_NUM;         /*STS数*/
    m_iPressDevNum = MAX_PRESS_NUM;     /*压力传感器数*/
    m_iLevelDevNum = MAX_LEVEL_NUM;     /*液位传感器数*/
    m_iPvDevNum = MAX_PV_NUM;           /*光伏数*/

    m_iRes1DevNum = MAX_RES1_NUM;       /*预留设备1数*/
    m_iAppDevNum = MAX_APP_NUM;         /*进程APP数*/
    m_pRTDBData = 0;

	if (m_iBattStackNum <= 0)
	{
        emit addLog(RUN_LOG_FAILED, "The param m_iBattStackNum <= 0, Set m_iBattStackNum = 1");
		m_iBattStackNum = 1;
	}

	if (m_iBattBankNum <= 0)
	{
        emit addLog(RUN_LOG_FAILED, "The param m_iBattBankNum <= 0, Set m_iBattBankNum = 1");
		m_iBattBankNum = 1;
	}

    if (m_iBattRegNum <= 0)
	{
        emit addLog(RUN_LOG_FAILED, "The param m_iBattRegNum <= 0, Set m_iBattRegNum = 1");
        m_iBattRegNum = 1;
	}

	// init some variables
    m_pSharedMemery = new QSharedMemory(keyName, parent);
}

CRTDB::~CRTDB()
{
	if (0 != m_pSharedMemery)
	{
		delete m_pSharedMemery;
		m_pSharedMemery = 0;
	}
}

//创建内存数据库，true创建成功，false创建失败
bool CRTDB::CreateRtdb()
{
    if (1 == initRTDB(true))
    {
        return true;
    }
    else
    {
        return false;
    }
}

//附着加载内存数据库,true加载成功,false加载失败
bool CRTDB::AttachRtdb()
{
    if (2 == initRTDB(true))
    {
        return true;
    }
    else
    {
        return false;
    }
}

//  1 - Create RTDB OK
//  2 - Open RTDB OK
// -1 - FAILED
// -2 - RTDB has been created or opened
int CRTDB::initRTDB(bool bReadWrite /*= false*/)
{
	int iRetVal = -1;
	QSharedMemory::AccessMode mode = QSharedMemory::ReadOnly;

    if (0 == m_pRTDBData)
	{
		emit addLog(RUN_LOG_FAILED, "RTDB has been created or opened.");
		iRetVal = -2;
	}

    m_iSize = 0;
	if (bReadWrite)
	{
		mode = QSharedMemory::ReadWrite;
        m_hashType2Size[MEM_BA_STACK] = m_iSize;
        m_iSize = m_iBattStackNum * sizeof(CBattStack); //电池堆
        m_hashType2Size[MEM_BA_REG] = m_iSize;
        m_iSize += m_iBattRegNum * sizeof(CBAComReg); //寄存器
        m_hashType2Size[MEM_BA_BANK] = m_iSize;
        m_iSize += m_iBattBankNum * sizeof(CBattBank); //电池簇
        m_hashType2Size[MEM_BA_ALARM] = m_iSize;
        m_iSize += m_iBattAlarmNum * sizeof(CBankAlarm); //电池簇告警
        m_hashType2Size[MEM_DEV_PCS] = m_iSize;
        m_iSize += m_iPcsDevNum * sizeof(CPcsData); //PCS逆变器

        m_hashType2Size[MEM_DEV_METER] = m_iSize;
        m_iSize += m_iMeterDevNum * sizeof(CMeterData); //电表
        m_hashType2Size[MEM_DEV_FIRE] = m_iSize;
        m_iSize += m_iFireDevNum * sizeof(CFireData); //消防
        m_hashType2Size[MEM_DEV_AC] = m_iSize;
        m_iSize += m_iAcDevNum * sizeof(CAcData); //空调
        m_hashType2Size[MEM_DEV_TRAN] = m_iSize;
        m_iSize += m_iTransDevNum * sizeof(CTransData); //变压器
        m_hashType2Size[MEM_DEV_ADAMIO] = m_iSize;
        m_iSize += m_iAdamIODevNum * sizeof(CAdamIOData); //研华IO

        m_hashType2Size[MEM_DEV_IO] = m_iSize;
        m_iSize += m_iIoDevNum * sizeof(CIoData); //IO模块
        m_hashType2Size[MEM_DEV_LC] = m_iSize;
        m_iSize += m_iLcDevNum * sizeof(CLcData); //液冷
        m_hashType2Size[MEM_DEV_EC] = m_iSize;
        m_iSize += m_iEcDevNum * sizeof(CEcData); //环控
        m_hashType2Size[MEM_DEV_TH] = m_iSize;
        m_iSize += m_iThDevNum * sizeof(CThData); //温湿度
        m_hashType2Size[MEM_DEV_DH] = m_iSize;
        m_iSize += m_iDhDevNum * sizeof(CDhData); //除湿

        m_hashType2Size[MEM_DEV_WATER] = m_iSize;
        m_iSize += m_iWaterDevNum * sizeof(CWaterData); //水浸
        m_hashType2Size[MEM_DEV_DOOR] = m_iSize;
        m_iSize += m_iDoorDevNum * sizeof(CDoorData); //门禁
        m_hashType2Size[MEM_DEV_DCDC] = m_iSize;
        m_iSize += m_iDcdcDevNum * sizeof(CDcdcData); //DCDC
        m_hashType2Size[MEM_DEV_CP] = m_iSize;
        m_iSize += m_iCpDevNum * sizeof(CCpData); //充电桩
        m_hashType2Size[MEM_DEV_GAS] = m_iSize;
        m_iSize += m_iGasDevNum * sizeof(CGasData); //可燃气体

        m_hashType2Size[MEM_DEV_UPS] = m_iSize;
        m_iSize += m_iUpsDevNum * sizeof(CUpsData); //UPS
        m_hashType2Size[MEM_DEV_LED] = m_iSize;
        m_iSize += m_iLedDevNum * sizeof(CLedData); //LED灯
        m_hashType2Size[MEM_DEV_FAN] = m_iSize;
        m_iSize += m_iFanDevNum * sizeof(CFanData); //风扇
        m_hashType2Size[MEM_DEV_STS] = m_iSize;
        m_iSize += m_iStsDevNum * sizeof(CStsData); //STS
        m_hashType2Size[MEM_DEV_PRESS] = m_iSize;
        m_iSize += m_iPressDevNum * sizeof(CPressData); //压力传感器数
        m_hashType2Size[MEM_DEV_LEVEL] = m_iSize;
        m_iSize += m_iLevelDevNum * sizeof(CPressData); //液位传感器数
        m_hashType2Size[MEM_DEV_PV] = m_iSize;
        m_iSize += m_iPvDevNum * sizeof(CPvData); //光伏数

        m_hashType2Size[MEM_DEV_RES1] = m_iSize;
        m_iSize += m_iRes1DevNum * sizeof(CReserve1Data); //预留设备1
        m_hashType2Size[MEM_DEV_APP] = m_iSize;
        m_iSize += m_iAppDevNum * sizeof(CAppData); //进程APP
		
        if (m_pSharedMemery->create(m_iSize, mode))
		{
            qDebug("create rtdb ok. m_iBattStackNum=%d m_iBattRegNum=%d m_iBattBankNum=%d m_iBattAlarmNum=%d m_iPcsDevNum=%d", m_iBattStackNum, m_iBattRegNum, m_iBattBankNum, m_iBattAlarmNum, m_iPcsDevNum);
            qDebug("create rtdb ok. m_iMeterDevNum=%d m_iFireDevNum=%d m_iAcDevNum=%d m_iTransDevNum=%d", m_iMeterDevNum, m_iFireDevNum, m_iAcDevNum, m_iTransDevNum);
            qDebug("create rtdb ok. m_iAdamIODevNum=%d m_iLcDevNum=%d m_iEcDevNum=%d m_iThDevNum=%d", m_iAdamIODevNum, m_iLcDevNum, m_iEcDevNum, m_iThDevNum);
            qDebug("create rtdb ok. m_iDhDevNum=%d m_iWaterDevNum=%d m_iDoorDevNum=%d m_iDcdcDevNum=%d", m_iDhDevNum, m_iWaterDevNum, m_iDoorDevNum, m_iDcdcDevNum);
            qDebug("create rtdb ok. m_iCpDevNum=%d m_iGasDevNum=%d m_iUpsDevNum=%d m_iRes1DevNum = %d", m_iCpDevNum, m_iGasDevNum, m_iUpsDevNum, m_iRes1DevNum);
            qDebug("create rtdb ok. m_iLedDevNum=%d m_iFanDevNum=%d m_iStsDevNum=%d m_iPressDevNum = %d m_iLevelDevNum = %d", m_iLedDevNum, m_iFanDevNum, m_iStsDevNum, m_iPressDevNum, m_iLevelDevNum);
            qDebug("create rtdb ok. m_iPvDevNum=%d m_iAppDevNum=%d m_iSize=%d", m_iPvDevNum, m_iAppDevNum, m_iSize);

            emit addLog(RUN_LOG_FAILED, QString("Create RTDB(BattStackNum = %1, BANum = %2, BattBankNum = %3) OK.").arg(m_iBattStackNum).arg(m_iBattRegNum).arg(m_iBattBankNum));

			m_pRTDBData = (char*)m_pSharedMemery->data();
            memset(m_pRTDBData, 0, m_iSize);

			iRetVal = 1;
		}
	}

	if (iRetVal < 0) //创建失败 或者 只读
	{
		// have existed	
		if (m_pSharedMemery->attach(mode))
		{
            qDebug("open rtdb ok. m_iBattStackNum=%d m_iBattRegNum=%d m_iBattBankNum=%d m_iBattAlarmNum=%d m_iPcsDevNum=%d", m_iBattStackNum, m_iBattRegNum, m_iBattBankNum, m_iBattAlarmNum, m_iPcsDevNum);
            qDebug("open rtdb ok. m_iMeterDevNum=%d m_iFireDevNum=%d m_iAcDevNum=%d m_iTransDevNum=%d", m_iMeterDevNum, m_iFireDevNum, m_iAcDevNum, m_iTransDevNum);
            qDebug("open rtdb ok. m_iAdamIODevNum=%d m_iLcDevNum=%d m_iEcDevNum=%d m_iThDevNum=%d", m_iAdamIODevNum, m_iLcDevNum, m_iEcDevNum, m_iThDevNum);
            qDebug("open rtdb ok. m_iDhDevNum=%d m_iWaterDevNum=%d m_iDoorDevNum=%d m_iDcdcDevNum=%d", m_iDhDevNum, m_iWaterDevNum, m_iDoorDevNum, m_iDcdcDevNum);
            qDebug("open rtdb ok. m_iCpDevNum=%d m_iGasDevNum=%d m_iUpsDevNum=%d m_iRes1DevNum = %d", m_iCpDevNum, m_iGasDevNum, m_iUpsDevNum, m_iRes1DevNum);
            qDebug("open rtdb ok. m_iLedDevNum=%d m_iFanDevNum=%d m_iStsDevNum=%d m_iPressDevNum = %d m_iLevelDevNum = %d", m_iLedDevNum, m_iFanDevNum, m_iStsDevNum, m_iPressDevNum, m_iLevelDevNum);
            qDebug("open rtdb ok. m_iPvDevNum=%d m_iAppDevNum=%d m_iSize=%d", m_iPvDevNum, m_iAppDevNum, m_iSize);
            emit addLog(RUN_LOG_FAILED, "Open RTDB OK.");

			m_pRTDBData = (char*)m_pSharedMemery->data();
			iRetVal = 2;
		}
		else
		{
			emit addLog(RUN_LOG_FAILED, "Init RTDB FAILED. Err Inof:" + m_pSharedMemery->errorString());
		}
	}

	return iRetVal;
}

//获取共享内存的总大小,单位为byte
int CRTDB::GetSize()
{
    return m_iSize;
}

// -1 - 找不到寄存器
//  0 - OK
int CRTDB::getComInputRegValBA(int iBAIndex, quint16 usRegAddr, quint16& usVal)
{
    if (iBAIndex < 0 || iBAIndex >= m_iBattRegNum)
	{
		return -1;
	}

    if (usRegAddr < 1 || usRegAddr > 100)
    {
        return -1;
    }

    if (0 == m_pRTDBData)
    {
        return -1;
    }

    int size = m_hashType2Size[MEM_BA_REG];
    size += iBAIndex * sizeof(CBAComReg); //寄存器
    CBAComReg* pBAComReg = (CBAComReg*)(m_pRTDBData + size);
	if (0 != pBAComReg)
	{
		usVal = pBAComReg->usComInputRegVal[usRegAddr - 1];

		return 0;
	}

	return -1;
}

// -1 - 找不到寄存器
//  0 - OK
int CRTDB::getComHoldRegValBA(int iBAIndex, quint16 usRegAddr, quint16& usVal)
{
    if (iBAIndex < 0 || iBAIndex >= m_iBattRegNum)
	{
		return -1;
	}

    if (usRegAddr < 1 || usRegAddr > 100)
    {
        return -1;
    }

    if (0 == m_pRTDBData)
    {
        return -1;
    }

    int size = m_hashType2Size[MEM_BA_REG];
    size += iBAIndex * sizeof(CBAComReg); //寄存器
    CBAComReg* pBAComReg = (CBAComReg*)(m_pRTDBData + size);
	if (0 != pBAComReg)
	{
		usVal = pBAComReg->usComHoldRegVal[usRegAddr - 1];

		return 0;
	}

	return -1;
}

//获取电池堆 (iBattStackIndex>=0 && iBattStackIndex<1)
CBattStack* CRTDB::getStack(int iBattStackIndex)
{
    if (iBattStackIndex < 0 || iBattStackIndex >= m_iBattStackNum)
	{
        return 0;
	}

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_BA_STACK];
    size += iBattStackIndex * sizeof(CBattStack);

    return (CBattStack*)(m_pRTDBData + size);
}

CBattStack* CRTDB::getStack0()
{
    return getStack(0);
}

//获取电池簇/组 (iBattBankIndex>=0 && iBattStackIndex<MAX_BANK_NUM)
CBattBank* CRTDB::getBank(int iBattBankIndex)
{
    if (iBattBankIndex < 0 || iBattBankIndex >= m_iBattBankNum)
	{
        return 0;
	}

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_BA_BANK];
    size += iBattBankIndex * sizeof(CBattBank); //电池簇

    return (CBattBank*)(m_pRTDBData + size);
}

CBattBank* CRTDB::getBank0()
{
    return getBank(0);
}

CBattBank* CRTDB::getBank1()
{
    return getBank(1);
}

CBattBank* CRTDB::getBank2()
{
    return getBank(2);
}

CBattBank* CRTDB::getBank3()
{
    return getBank(3);
}

//获取电池寄存器 (iBattRegIndex>=0 && iBattStackIndex<MAX_REG_NUM)
CBAComReg* CRTDB::getBAComReg(int iBattRegIndex)
{
    if (iBattRegIndex < 0 || iBattRegIndex >= m_iBattRegNum)
	{
        return 0;
	}

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_BA_REG];
    size += iBattRegIndex * sizeof(CBAComReg); //寄存器

    return (CBAComReg*)(m_pRTDBData + size);
}

CBAComReg* CRTDB::getBAComReg0()
{
    return getBAComReg(0);
}

CBAComReg* CRTDB::getBAComReg1()
{
    return getBAComReg(1);
}

CBAComReg* CRTDB::getBAComReg2()
{
    return getBAComReg(2);
}

CBAComReg* CRTDB::getBAComReg3()
{
    return getBAComReg(3);
}

//获取PCS逆变器设备 (iPcsDevIndex>=0 && iPcsDevIndex<MAX_PCS_NUM)
CPcsData* CRTDB::getPcs(int iPcsDevIndex)
{
    if (iPcsDevIndex < 0 || iPcsDevIndex >= m_iPcsDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_PCS];
    size += iPcsDevIndex * sizeof(CPcsData); //PCS逆变器

    return (CPcsData*)(m_pRTDBData + size);
}

CPcsData* CRTDB::getPcs0()
{
    return getPcs(0);
}

CPcsData* CRTDB::getPcs1()
{
    return getPcs(1);
}

CPcsData* CRTDB::getPcs2()
{
    return getPcs(2);
}

CPcsData* CRTDB::getPcs3()
{
    return getPcs(3);
}

//获取电表设备 (iMeterDevIndex>=0 && iMeterDevIndex<MAX_METER_NUM)
CMeterData* CRTDB::getMeter(int iMeterDevIndex)
{
    if (iMeterDevIndex < 0 || iMeterDevIndex >= m_iMeterDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_METER];
    size += iMeterDevIndex * sizeof(CMeterData); //电表
	
    return (CMeterData*)(m_pRTDBData + size);
}

CMeterData* CRTDB::getMeter0()
{
    return getMeter(0);
}

CMeterData* CRTDB::getMeter1()
{
    return getMeter(1);
}

CMeterData* CRTDB::getMeter2()
{
    return getMeter(2);
}

CMeterData* CRTDB::getMeter3()
{
    return getMeter(3);
}

//获取消防设备 (iFireDevIndex>=0 && iFireDevIndex<MAX_FIRE_NUM)
CFireData* CRTDB::getFireDev(int iFireDevIndex)
{
    if (iFireDevIndex < 0 || iFireDevIndex >= m_iFireDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_FIRE];
    size += iFireDevIndex * sizeof(CFireData); //消防
	
    return (CFireData*)(m_pRTDBData + size);
}

CFireData* CRTDB::getFireDev0()
{
    return getFireDev(0);
}

CFireData* CRTDB::getFireDev1()
{
    return getFireDev(1);
}

CFireData* CRTDB::getFireDev2()
{
    return getFireDev(2);
}

CFireData* CRTDB::getFireDev3()
{
    return getFireDev(3);
}

//获取空调设备 (iAcDevIndex>=0 && iAcDevIndex<MAX_AC_NUM)
CAcData* CRTDB::getAcDev(int iAcDevIndex)
{
    if (iAcDevIndex < 0 || iAcDevIndex >= m_iAcDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_AC];
    size += iAcDevIndex * sizeof(CAcData); //空调
	
    return (CAcData*)(m_pRTDBData + size);
}

CAcData* CRTDB::getAcDev0()
{
    return getAcDev(0);
}

CAcData* CRTDB::getAcDev1()
{
    return getAcDev(1);
}

CAcData* CRTDB::getAcDev2()
{
    return getAcDev(2);
}

CAcData* CRTDB::getAcDev3()
{
    return getAcDev(3);
}

//获取变压器设备 (iTransDevIndex>=0 && iTransDevIndex<2)
CTransData* CRTDB::getTransDev(int iTransDevIndex)
{
    if (iTransDevIndex < 0 || iTransDevIndex >= m_iTransDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_TRAN];
    size += iTransDevIndex * sizeof(CTransData); //变压器
	
    return (CTransData*)(m_pRTDBData + size);
}

CTransData* CRTDB::getTransDev0()
{
    return getTransDev(0);
}

CTransData* CRTDB::getTransDev1()
{
    return getTransDev(1);
}

//获取研华IO
CAdamIOData* CRTDB::getAdamIODev()
{
    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_ADAMIO];
	
	return (CAdamIOData*)(m_pRTDBData + size);
}

//获取IO模块 Dev0为屏后IO,Dev1为扩展IO (iIoDevIndex>=0 && iIoDevIndex<2)
CIoData* CRTDB::getIoDev(int iIoDevIndex)
{
    if (iIoDevIndex < 0 || iIoDevIndex >= m_iIoDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_IO];
    size += iIoDevIndex * sizeof(CIoData); //IO模块
	
    return (CIoData*)(m_pRTDBData + size);
}

CIoData* CRTDB::getIoDev0()
{
    return getIoDev(0);
}

CIoData* CRTDB::getIoDev1()
{
    return getIoDev(1);
}

//获取液冷设备 (iLcDevIndex>=0 && iLcDevIndex<MAX_LC_NUM)
CLcData* CRTDB::getLcDev(int iLcDevIndex)
{
    if (iLcDevIndex < 0 || iLcDevIndex >= m_iLcDevNum)
	{
        return 0;
	}

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_LC];
    size += iLcDevIndex * sizeof(CLcData); //液冷

	return (CLcData*)(m_pRTDBData + size);
}

CLcData* CRTDB::getLcDev0()
{
    return getLcDev(0);
}

CLcData* CRTDB::getLcDev1()
{
    return getLcDev(1);
}

CLcData* CRTDB::getLcDev2()
{
    return getLcDev(2);
}

CLcData* CRTDB::getLcDev3()
{
    return getLcDev(3);
}

//获取环控设备 (iEcDevIndex>=0 && iEcDevIndex<MAX_EC_NUM)
CEcData* CRTDB::getEcDev(int iEcDevIndex)
{
    if (iEcDevIndex < 0 || iEcDevIndex >= m_iEcDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_EC];
    size += iEcDevIndex * sizeof(CEcData); //环控

    return (CEcData*)(m_pRTDBData + size);
}

CEcData* CRTDB::getEcDev0()
{
    return getEcDev(0);
}

CEcData* CRTDB::getEcDev1()
{
    return getEcDev(1);
}

CEcData* CRTDB::getEcDev2()
{
    return getEcDev(2);
}

CEcData* CRTDB::getEcDev3()
{
    return getEcDev(3);
}

//获取温湿度设备 (iThDevIndex>=0 && iThDevIndex<MAX_TH_NUM)
CThData* CRTDB::getThDev(int iThDevIndex)
{
    if (iThDevIndex < 0 || iThDevIndex >= m_iThDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_TH];
    size += iThDevIndex * sizeof(CThData); //温湿度

    return (CThData*)(m_pRTDBData + size);
}

CThData* CRTDB::getThDev0()
{
    return getThDev(0);
}

CThData* CRTDB::getThDev1()
{
    return getThDev(1);
}

CThData* CRTDB::getThDev2()
{
    return getThDev(2);
}

CThData* CRTDB::getThDev3()
{
    return getThDev(3);
}

//获取除湿设备 (iDhDevIndex>=0 && iDhDevIndex<MAX_DH_NUM)
CDhData* CRTDB::getDhDev(int iDhDevIndex)
{
    if (iDhDevIndex < 0 || iDhDevIndex >= m_iDhDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_DH];
    size += iDhDevIndex * sizeof(CDhData); //除湿

    return (CDhData*)(m_pRTDBData + size);
}

CDhData* CRTDB::getDhDev0()
{
    return getDhDev(0);
}

CDhData* CRTDB::getDhDev1()
{
    return getDhDev(1);
}

CDhData* CRTDB::getDhDev2()
{
    return getDhDev(2);
}

CDhData* CRTDB::getDhDev3()
{
    return getDhDev(3);
}

//获取水浸设备 (iWaterDevIndex>=0 && iWaterDevIndex<MAX_WATER_NUM)
CWaterData* CRTDB::getWaterDev(int iWaterDevIndex)
{
    if (iWaterDevIndex < 0 || iWaterDevIndex >= m_iWaterDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_WATER];
    size += iWaterDevIndex * sizeof(CWaterData); //水浸

    return (CWaterData*)(m_pRTDBData + size);
}

CWaterData* CRTDB::getWaterDev0()
{
    return getWaterDev(0);
}

CWaterData* CRTDB::getWaterDev1()
{
    return getWaterDev(1);
}

CWaterData* CRTDB::getWaterDev2()
{
    return getWaterDev(2);
}

CWaterData* CRTDB::getWaterDev3()
{
    return getWaterDev(3);
}

//获取门禁设备 (iDoorDevIndex>=0 && iDoorDevIndex<MAX_DOOR_NUM)
CDoorData* CRTDB::getDoorDev(int iDoorDevIndex)
{
    if (iDoorDevIndex < 0 || iDoorDevIndex >= m_iDoorDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_DOOR];
    size += iDoorDevIndex * sizeof(CDoorData); //门禁

    return (CDoorData*)(m_pRTDBData + size);
}

CDoorData* CRTDB::getDoorDev0()
{
    return getDoorDev(0);
}

CDoorData* CRTDB::getDoorDev1()
{
    return getDoorDev(1);
}

CDoorData* CRTDB::getDoorDev2()
{
    return getDoorDev(2);
}

CDoorData* CRTDB::getDoorDev3()
{
    return getDoorDev(3);
}

//获取DCDC设备 (iDcdcDevIndex>=0 && iDcdcDevIndex<MAX_DCDC_NUM)
CDcdcData* CRTDB::getDcdcDev(int iDcdcDevIndex)
{
    if (iDcdcDevIndex < 0 || iDcdcDevIndex >= m_iDcdcDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_DCDC];
    size += iDcdcDevIndex * sizeof(CDcdcData); //DCDC

    return (CDcdcData*)(m_pRTDBData + size);
}

CDcdcData* CRTDB::getDcdcDev0()
{
    return getDcdcDev(0);
}

CDcdcData* CRTDB::getDcdcDev1()
{
    return getDcdcDev(1);
}

CDcdcData* CRTDB::getDcdcDev2()
{
    return getDcdcDev(2);
}

CDcdcData* CRTDB::getDcdcDev3()
{
    return getDcdcDev(3);
}

//获取充电桩设备 (iCpDevIndex>=0 && iCpDevIndex<MAX_PILE_NUM)
CCpData* CRTDB::getCpDev(int iCpDevIndex)
{
    if (iCpDevIndex < 0 || iCpDevIndex >= m_iCpDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_CP];
    size += iCpDevIndex * sizeof(CCpData); //充电桩

    return (CCpData*)(m_pRTDBData + size);
}

CCpData* CRTDB::getCpDev0()
{
    return getCpDev(0);
}

CCpData* CRTDB::getCpDev1()
{
    return getCpDev(1);
}

CCpData* CRTDB::getCpDev2()
{
    return getCpDev(2);
}

CCpData* CRTDB::getCpDev3()
{
    return getCpDev(3);
}

//获取可燃气体设备 (iGasDevIndex>=0 && iGasDevIndex<MAX_GAS_NUM)
CGasData* CRTDB::getGasDev(int iGasDevIndex)
{
    if (iGasDevIndex < 0 || iGasDevIndex >= m_iGasDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_GAS];
    size += iGasDevIndex * sizeof(CGasData); //可燃气体

    return (CGasData*)(m_pRTDBData + size);
}

CGasData* CRTDB::getGasDev0()
{
    return getGasDev(0);
}

CGasData* CRTDB::getGasDev1()
{
    return getGasDev(1);
}

CGasData* CRTDB::getGasDev2()
{
    return getGasDev(2);
}

CGasData* CRTDB::getGasDev3()
{
    return getGasDev(3);
}

//获取UPS (iUpsDevIndex>=0 && iUpsDevIndex<MAX_UPS_NUM)
CUpsData* CRTDB::getUpsDev(int iUpsDevIndex)
{
    if (iUpsDevIndex < 0 || iUpsDevIndex >= m_iUpsDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_UPS];
    size += iUpsDevIndex * sizeof(CUpsData); //UPS

    return (CUpsData*)(m_pRTDBData + size);
}

CUpsData* CRTDB::getUpsDev0()
{
    return getUpsDev(0);
}

CUpsData* CRTDB::getUpsDev1()
{
    return getUpsDev(1);
}

CUpsData* CRTDB::getUpsDev2()
{
    return getUpsDev(2);
}

CUpsData* CRTDB::getUpsDev3()
{
    return getUpsDev(3);
}

//获取LED灯 (iLedDevIndex>=0 && iLedDevIndex<MAX_LED_NUM)
CLedData* CRTDB::getLedDev(int iLedDevIndex)
{
    if (iLedDevIndex < 0 || iLedDevIndex >= m_iLedDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_LED];
    size += iLedDevIndex * sizeof(CLedData); //LED灯

    return (CLedData*)(m_pRTDBData + size);
}

CLedData* CRTDB::getLedDev0()
{
    return getLedDev(0);
}

CLedData* CRTDB::getLedDev1()
{
    return getLedDev(1);
}

CLedData* CRTDB::getLedDev2()
{
    return getLedDev(2);
}

CLedData* CRTDB::getLedDev3()
{
    return getLedDev(3);
}

//获取风扇灯 (iFanDevIndex>=0 && iFanDevIndex<MAX_FAN_NUM)
CFanData* CRTDB::getFanDev(int iFanDevIndex)
{
    if (iFanDevIndex < 0 || iFanDevIndex >= m_iFanDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_FAN];
    size += iFanDevIndex * sizeof(CFanData); //风扇

    return (CFanData*)(m_pRTDBData + size);
}

CFanData* CRTDB::getFanDev0()
{
    return getFanDev(0);
}

CFanData* CRTDB::getFanDev1()
{
    return getFanDev(1);
}

CFanData* CRTDB::getFanDev2()
{
    return getFanDev(2);
}

CFanData* CRTDB::getFanDev3()
{
    return getFanDev(3);
}

//获取STS (iStsDevIndex>=0 && iStsDevIndex<MAX_STS_NUM)
CStsData* CRTDB::getStsDev(int iStsDevIndex)
{
    if (iStsDevIndex < 0 || iStsDevIndex >= m_iStsDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_STS];
    size += iStsDevIndex * sizeof(CStsData); //STS

    return (CStsData*)(m_pRTDBData + size);
}

CStsData* CRTDB::getStsDev0()
{
    return getStsDev(0);
}

CStsData* CRTDB::getStsDev1()
{
    return getStsDev(1);
}

CStsData* CRTDB::getStsDev2()
{
    return getStsDev(2);
}

CStsData* CRTDB::getStsDev3()
{
    return getStsDev(3);
}

//获取压力传感器 (iPressDevIndex>=0 && iPressDevIndex<MAX_PRESS_NUM)
CPressData* CRTDB::getPressDev(int iPressDevIndex)
{
    if (iPressDevIndex < 0 || iPressDevIndex >= m_iPressDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_PRESS];
    size += iPressDevIndex * sizeof(CPressData); //压力传感器

    return (CPressData*)(m_pRTDBData + size);
}

CPressData* CRTDB::getPressDev0()
{
    return getPressDev(1);
}

CPressData* CRTDB::getPressDev1()
{
    return getPressDev(1);
}

CPressData* CRTDB::getPressDev2()
{
    return getPressDev(2);
}

CPressData* CRTDB::getPressDev3()
{
    return getPressDev(3);
}

//获取液位传感器 (iLevelDevIndex>=0 && iLevelDevIndex<MAX_LEVEL_NUM)
CLevelData* CRTDB::getLevelDev(int iLevelDevIndex)
{
    if (iLevelDevIndex < 0 || iLevelDevIndex >= m_iLevelDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_LEVEL];
    size += iLevelDevIndex * sizeof(CLevelData); //液位传感器

    return (CLevelData*)(m_pRTDBData + size);
}

CLevelData* CRTDB::getLevelDev0()
{
    return getLevelDev(0);
}

CLevelData* CRTDB::getLevelDev1()
{
    return getLevelDev(1);
}

CLevelData* CRTDB::getLevelDev2()
{
    return getLevelDev(2);
}

CLevelData* CRTDB::getLevelDev3()
{
    return getLevelDev(3);
}

//获取光伏 (iPvDevIndex>=0 && iPvDevIndex<MAX_PV_NUM)
CPvData* CRTDB::getPvDev(int iPvDevIndex)
{
    if (iPvDevIndex < 0 || iPvDevIndex >= m_iPvDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_PV];
    size += iPvDevIndex * sizeof(CPvData); //光伏

    return (CPvData*)(m_pRTDBData + size);
}

CPvData* CRTDB::getPvDev0()
{
    return getPvDev(0);
}

CPvData* CRTDB::getPvDev1()
{
    return getPvDev(1);
}

CPvData* CRTDB::getPvDev2()
{
    return getPvDev(2);
}

CPvData* CRTDB::getPvDev3()
{
    return getPvDev(3);
}

//获取预留设备1 (iRes1DevIndex>=0 && iRes1DevIndex<MAX_RES1_NUM)
CReserve1Data* CRTDB::getRes1Data(int iRes1DevIndex)
{
    if (iRes1DevIndex < 0 || iRes1DevIndex >= m_iRes1DevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_RES1];
    size += iRes1DevIndex * sizeof(CReserve1Data); //预留设备1

    return (CReserve1Data*)(m_pRTDBData + size);
}

//获取进程APP (iAppDevIndex>=0 && iAppDevIndex<MAX_APP_NUM)
CAppData* CRTDB::getAppData(int iAppDevIndex)
{
    if (iAppDevIndex < 0 || iAppDevIndex >= m_iAppDevNum)
    {
        return 0;
    }

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_DEV_APP];
    size += iAppDevIndex * sizeof(CAppData); //进程APP

    return (CAppData*)(m_pRTDBData + size);
}

// Alarm
//获取电池组告警
CBankAlarm* CRTDB::getBankAlarm(int iBattBankIndex)
{
    if (iBattBankIndex < 0 || iBattBankIndex >= m_iBattAlarmNum)
	{
        return 0;
	}

    if (0 == m_pRTDBData)
    {
        return 0;
    }

    int size = m_hashType2Size[MEM_BA_ALARM];
    size += iBattBankIndex * sizeof(CBankAlarm); //电池簇告警

    return (CBankAlarm*)(m_pRTDBData + size);
}

SAlarm* CRTDB::getFirstAlarm(int iBattBankIndex)
{
	return getFirstAlarm(getBankAlarm(iBattBankIndex));
}

SAlarm* CRTDB::getFirstAlarm(CBankAlarm* pBankAlarm)
{
    if (0 == pBankAlarm)
	{
        return 0;
	}

	quint16 usFirstNo = pBankAlarm->usFirstAlarmNo; 

	if (ALARM_LAST_NO == usFirstNo || usFirstNo >= BANK_ALARM_MAX_NUM)
	{
        return 0;
	}

	return &(pBankAlarm->bankAlarm[usFirstNo]);
}

SAlarm* CRTDB::getAlarm(CBankAlarm* pBankAlarm, quint16 usNo)
{
    if (0 == pBankAlarm)
    {
        return 0;
    }

    if (ALARM_LAST_NO == usNo || usNo >= BANK_ALARM_MAX_NUM)
	{
        return 0;
	}

	return &(pBankAlarm->bankAlarm[usNo]);
}

SAlarmParam* CRTDB::getAlarmParam(int iBattBankIndex)
{
    SAlarmParam* pParam = 0;
	CBankAlarm* pBankAlarm = getBankAlarm(iBattBankIndex);
	if (0 != pBankAlarm)
	{
		pParam = &(pBankAlarm->param);
	}

	return pParam;
}

void CRTDB::setDryWetCtrl(CBattBank* pBattBank, quint16 usDry1Ctrl, quint16 usDry2Ctrl, quint16 usDry3Ctrl, quint16 usDry4Ctrl, quint16 usWetCtrl, int iDry1CtrlNum, int iDry2CtrlNum, int iDry3CtrlNum, int iDry4CtrlNum, int iWetCtrlNum)
{
	setDryCtrl1(pBattBank, usDry1Ctrl, usDry2Ctrl, iDry1CtrlNum, iDry2CtrlNum);
	setDryCtrl2(pBattBank, usDry3Ctrl, usDry4Ctrl, iDry3CtrlNum, iDry4CtrlNum);
	setWetCtrl(pBattBank, usWetCtrl, iWetCtrlNum);
}

void CRTDB::setDryCtrl1(CBattBank* pBattBank, quint16 usDry1Ctrl, quint16 usDry2Ctrl, int iDry1CtrlNum, int iDry2CtrlNum)
{
	if (0 != pBattBank)
	{
		if (iDry1CtrlNum != 0)
		{
			pBattBank->usDry1Ctrl = usDry1Ctrl;
			pBattBank->iDry1CtrlNum = iDry1CtrlNum;
		}
		
		if (iDry2CtrlNum != 0)
		{
			pBattBank->usDry2Ctrl = usDry2Ctrl;
			pBattBank->iDry2CtrlNum = iDry2CtrlNum;
		}
	}
}

void CRTDB::setDryCtrl2(CBattBank* pBattBank, quint16 usDry3Ctrl, quint16 usDry4Ctrl, int iDry3CtrlNum, int iDry4CtrlNum)
{
	if (0 != pBattBank)
	{
		if (iDry3CtrlNum != 0)
		{
			pBattBank->usDry3Ctrl = usDry3Ctrl;
			pBattBank->iDry3CtrlNum = iDry3CtrlNum;
		}

		if (iDry4CtrlNum != 0)
		{
			pBattBank->usDry4Ctrl = usDry4Ctrl;
			pBattBank->iDry4CtrlNum = iDry4CtrlNum;
		}
	}
}

void CRTDB::setWetCtrl(CBattBank* pBattBank, quint16 usWetCtrl, int iWetCtrlNum)
{
	if (0 != pBattBank)
	{
		if (iWetCtrlNum != 0)
		{
			pBattBank->usWetCtrl = usWetCtrl;
			pBattBank->iWetCtrlNum = iWetCtrlNum;
		}
	}
}

